<?php

class MediaAction extends Action {

    /**
     * 媒体版本
     */
    public function index(){

        $Model = new Model();
        $Model->db("2","Db_Sxol_Home");

        //幻灯片5条
        $StrSqlMediaTJ = "SELECT ID, ChannelID, TypeID, Loca, Img1, Img2, Title, Url,area_name FROM vPub_Ad WHERE (ChannelID = 6) and (TypeID = 12) and (area_name='www') ORDER BY ID desc ";
        $DataMediaTJ   =  $Model->query( $StrSqlMediaTJ );

        //头条资讯
        $StrSqlMediaTJ = "SELECT TOP (7) *,CONVERT(varchar(100), PostTime, 120) as DateAndTime  FROM adv_Article WHERE (IsTop = 1) and (area_name='www') ORDER BY ID DESC";
        $DataArtTop    = $Model->query( $StrSqlMediaTJ );

        //----------------------------------------------------------------------------------------------------------------------------------------------------------------------
        //网络媒体
        $StrSqlLbMT1 = "SELECT TOP (7) ID, AdsName, ReImg, Miniature, GoUrl FROM adv_Media_Ads WHERE (Type1 = 1) AND (Recommend = 0)  and (area_name='www') ORDER BY OrderNum, ID DESC";
        $DataLbMT1   = $Model->query( $StrSqlLbMT1 );
        //电视媒体
        $StrSqlLbMT2 = "SELECT TOP (7) ID, AdsName, ReImg, Miniature, GoUrl FROM adv_Media_Ads WHERE (Type1 = 2) AND (Recommend = 0)  and (area_name='www') ORDER BY OrderNum, ID DESC";
        $DataLbMT2   = $Model->query( $StrSqlLbMT2 );
        //电台媒体
        $StrSqlLbMT3 = "SELECT TOP (7) ID, AdsName, ReImg, Miniature, GoUrl FROM adv_Media_Ads WHERE (Type1 = 3) AND (Recommend = 0)  and (area_name='www') ORDER BY OrderNum, ID DESC";
        $DataLbMT3   = $Model->query( $StrSqlLbMT3 );
        //平面媒体
        $StrSqlLbMT4 = "SELECT TOP (7) ID, AdsName, ReImg, Miniature, GoUrl FROM adv_Media_Ads WHERE (Type1 = 4) AND (Recommend = 0)  and (area_name='www') ORDER BY OrderNum, ID DESC";
        $DataLbMT4   = $Model->query( $StrSqlLbMT4 );
        //-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

        //三面翻
        $StrSqlLbMT77 = "SELECT TOP (7) ID, AdsName, ReImg, Miniature, GoUrl FROM adv_Media_Ads WHERE (Type1 = 7) AND (Type2 = 7) AND (Recommend = 0)  and (area_name='www') ORDER BY OrderNum, ID DESC";
        $DataLbMT77   = $Model->query( $StrSqlLbMT77 );
        //T型牌
        $StrSqlLbMT78 = "SELECT TOP (7) ID, AdsName, ReImg, Miniature, GoUrl FROM adv_Media_Ads WHERE (Type1 = 7) AND (Type2 = 8) AND (Recommend = 0)  and (area_name='www') ORDER BY OrderNum, ID DESC";
        $DataLbMT78   = $Model->query( $StrSqlLbMT78 );
        //楼顶广告牌
        $StrSqlLbMT714 = "SELECT TOP (7) ID, AdsName, ReImg, Miniature, GoUrl FROM adv_Media_Ads WHERE (Type1 = 7) AND (Type2 = 14) AND (Recommend = 0)  and (area_name='www') ORDER BY OrderNum, ID DESC";
        $DataLbMT714   = $Model->query( $StrSqlLbMT714 );
        //墙面
        $StrSqlLbMT710 = "SELECT TOP (7) ID, AdsName, ReImg, Miniature, GoUrl FROM adv_Media_Ads WHERE (Type1 = 7) AND (Type2 = 10) AND (Recommend = 0)  and (area_name='www') ORDER BY OrderNum, ID DESC";
        $DataLbMT710   = $Model->query( $StrSqlLbMT710 );
        //-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

        //候车亭
        $StrSqlLbMT711 = "SELECT TOP (7) ID, AdsName, ReImg, Miniature, GoUrl FROM adv_Media_Ads WHERE (Type1 = 7) AND (Type2 = 11) AND (Recommend = 0)  and (area_name='www') ORDER BY OrderNum, ID DESC";
        $DataLbMT711   = $Model->query( $StrSqlLbMT711 );
        //灯箱
        $StrSqlLbMT713 = "SELECT TOP (7) ID, AdsName, ReImg, Miniature, GoUrl FROM adv_Media_Ads WHERE (Type1 = 7) AND (Type2 = 13) AND (Recommend = 0)  and (area_name='www') ORDER BY OrderNum, ID DESC";
        $DataLbMT713   = $Model->query( $StrSqlLbMT713 );
        //LED屏
        $StrSqlLbMT79 = "SELECT TOP (7) ID, AdsName, ReImg, Miniature, GoUrl FROM adv_Media_Ads WHERE (Type1 = 7) AND (Type2 = 9) AND (Recommend = 0)  and (area_name='www') ORDER BY OrderNum, ID DESC";
        $DataLbMT79   = $Model->query( $StrSqlLbMT79 );
        //其他
        $StrSqlLbMT718 = "SELECT TOP (7) ID, AdsName, ReImg, Miniature, GoUrl FROM adv_Media_Ads WHERE (Type1 = 7) AND (Type2 = 18) AND (Recommend = 0)  and (area_name='www') ORDER BY OrderNum, ID DESC";
        $DataLbMT718   = $Model->query( $StrSqlLbMT718 );

        $Model->db(0);

        //精彩赏图
        $StrRanImg =  "select top 4 *, NewID() as random from (SELECT top 20 * FROM photo_class where Dth = 3 and area_name = '".C('APP_URL_AREA')."' ORDER BY id desc) pc2 order by random  ";
        $DataRanImg = $Model->query( $StrRanImg );

//        //最新SUV
//        $StrSqlGCSUV = "select top 4 MIN(ID) AS id, classid,classname from group_car where jibie Like '%SUV%' GROUP BY ClassID,classname ORDER BY id desc";
//        $DataGcSUV = $Model->query( $StrSqlGCSUV );
//
//        //最新轿车
//        $StrSqlGCCar = "select top 4 MIN(ID) AS id, classid,classname from group_car where jibie Like '%小型%' GROUP BY ClassID,classname ORDER BY id desc";
//        $DataGcCar = $Model->query( $StrSqlGCCar );


        //最新轿车
        //$this->assign('DataGcCar',$DataGcCar);
        //$this->assign('DataGcSUV',$DataGcSUV);

        $this->assign('DataMediaTJ',$DataMediaTJ);
        $this->assign('DataArtTop',$DataArtTop);
        $this->assign('DataLbMT1',$DataLbMT1);
        $this->assign('DataLbMT2',$DataLbMT2);
        $this->assign('DataLbMT3',$DataLbMT3);
        $this->assign('DataLbMT4',$DataLbMT4);
        $this->assign('DataLbMT77',$DataLbMT77);
        $this->assign('DataLbMT78',$DataLbMT78);
        $this->assign('DataLbMT714',$DataLbMT714);
        $this->assign('DataLbMT710',$DataLbMT710);
        $this->assign('DataLbMT711',$DataLbMT711);
        $this->assign('DataLbMT713',$DataLbMT713);
        $this->assign('DataLbMT79',$DataLbMT79);
        $this->assign('DataLbMT718',$DataLbMT718);

        $this->assign('DataRanImg',$DataRanImg);

        $this->display();
     }

    /**
     * -----媒体广告详情
     */
    public function MediaAdsDet($ID){
        $Model = new Model();
        $Model->db("2","Db_Sxol_Home");
        $StrSqlAdvAds = "SELECT * FROM vAdv_Ads WHERE (ID = ".$ID.") AND (Vis = 1)";
        $DataAdvAds   = $Model->query( $StrSqlAdvAds );
        if($DataAdvAds){
            $DataAdvAds[0]['Images'];
            $picArr = split('\,',$DataAdvAds[0]['Images']);
            if(count($picArr)>1){
                $DataAdvAds[0]['ImagesArr'] = $picArr;
            }else{
                $DataAdvAds[0]['ImagesArr'] = array(  $DataAdvAds[0]['Images'] );                ;
            }
            $this->assign('DataAdvAds',$DataAdvAds);
            $this->display();
        }else{
            $this->error('不存在该广告模块！');
        }

    }

    /**
     *汽车文章列表
     * @param $ClassID
     * @param int $pageSize
     * @param int $page
     */
    public function carartlist($ClassID,$pageSize = 20,$page = 0){

        $DataUc = $this-> unClass($ClassID);
        if($DataUc ){
            $DataUa = $this->searchArt($ClassID,$pageSize,$page);
            $ClassName = $this->getClassName($ClassID);

            $this->assign('ClassName',$ClassName);
            $this->assign('ClassID',$ClassID);
            $this->assign('DataUa',$DataUa);
            $this->display();
        }else{
            $this->error('请求的模块不存在！');
        }
    }

    /**
     *汽车文章搜索
     * @param $ClassID
     * @param int $pageSize
     * @param int $page
     */
    public function searArtlist($ClassID,$keyword,$pageSize = 20,$page = 0){

        if($ClassID != "" || $keyword !=""){

            $DataUa = $this->searchArt($ClassID,$pageSize,$page,$keyword);

            $this->assign("keyword",$keyword);
            $this->assign('ClassID',$ClassID);
            $this->assign('DataUa',$DataUa);
            $this->display();
        }else{
            $this->error('请求的模块不存在！');
        }
    }

    /**
     * 汽车的文章
     * @param $id
     */
    public function  carart($id){

        $Model = new Model(); // 实例化一个model对象 没有对应任何数据表
        $Model->execute("update Uni_Article set Hits=Hits+1 where id=".$id);
        $StrSqlUniArt = "select *,CONVERT(varchar(100), DateAndTime, 120) as DateAndTime from Uni_Article where id = ".$id." ORDER BY id desc";
        $DataUniArt = $Model->query( $StrSqlUniArt );

        if ($DataUniArt){

            //最新的相关的6条
            $StrSqlUniArtNew = "select top 6 * from Uni_Article where id <> ".$DataUniArt[0]["ID"]." and ClassID = '".$DataUniArt[0]["ClassID"]."' ORDER BY id desc";
            $DataUniArtNew= $Model->query( $StrSqlUniArtNew );

            //精彩赏图
            $StrRanFour =  "select top 4 *, NewID() as random from (SELECT top 20 * FROM photo_class where Dth = 3 and area_name = '".C('APP_URL_AREA')."' ORDER BY id desc) pc2 order by random  ";
            $DataRanFour= $Model->query( $StrRanFour );

            //字母品牌
            $DataGc = $this->CarZimu();

            $this->assign('DataGc',$DataGc);
            $this->assign('ClassName',$this->getClassName($DataUniArt[0]["ClassID"]));
            $this->assign('ClassID',$DataUniArt[0]["ClassID"]);
            $this->assign('DataRanFour',$DataRanFour);
            $this->assign('DataUniArtNew',$DataUniArtNew);
            $this->assign('DataUniArt',$DataUniArt)  ;
            $this->display();

        }else{
            $this->error('没有找到该文章！');
        }

    }

    /**
     * JSONP返回
     * @param $ClassID
     * @param int $pageSize
     * @param int $page
     */
    public function search($ClassID,$keyword,$pageSize = 10,$page = 0){
        $DataUc = $this -> searchArt($ClassID,$pageSize,$page,$keyword);
        if ($DataUc){
            $this->ajaxReturn($DataUc,"查询成功！",1,'JSONP');
        }else{
            $this->ajaxReturn(0,"没有数据了！",0,'JSONP');
        }
    }

    /**
     * 返回文章的类型
     * @param $ClassID
     * @return array|mixed
     */
    public function unClass ($ClassID){
        $Model = new Model();
        $StrSqlUc = "Select top 1 * From Uni_Class Where classid='".$ClassID."'";
        $DataUc = $Model->query( $StrSqlUc );
        return $DataUc;
    }

    /**
     * 分页返回文章列表
     * @param $ClassID
     * @param int $pageSize
     * @param int $page
     * @return array|mixed
     */
    public  function  searchArt($ClassID,$pageSize = 10,$page = 0,$Keyword = ""){
        $Model = new Model();
        $where = "1 = 1 ";

        if($ClassID != ""){
            $where = $where . " and ClassID='".$ClassID."'";
        }

        if($Keyword != ""){
            $where = $where . " and Title Like '%".$Keyword."%' and Keywords Like '%".$Keyword."%' ";
        }

        $where = $where . "and area_name='".C("APP_URL_AREA")."'";

        $StrSqlUc = "SELECT * from (select top ".intval($pageSize)*(intval($page)+1)." ID,Title,Keywords,CONVERT(varchar(100), DateAndTime, 120) as DateAndTime,IsTop,copyfrom,author,hits,Images,Images1,LinkUrl from Uni_article Where ".$where."  order by IsTop desc,ID desc) as art where  art.id not in (select top ".intval($pageSize)*intval($page)." ID from Uni_article Where ".$where."  order by IsTop desc,ID desc)";
        $DataUc = $Model->query( $StrSqlUc );

        $StrSqlTotal =  "select Count(ID) as count from Uni_article Where ".$where;
        $DataToal = $Model->query( $StrSqlTotal );

        $this->assign("DataToal",$DataToal);

        return $DataUc;
    }

    /**
     * 添加询价或试驾
     */
    public function AddShiJia(){
        if (!isset( $_POST [ 'carPinpai' ])){
            $this->ajaxReturn(0,"汽车品牌必须选择！",0,'JSON');
        }
        if (!isset( $_POST [ 'carType' ])){
            $this->ajaxReturn(0,"汽车款式必须选择！",0,'JSON');
        }
        if (!isset( $_POST [ 'tel' ])){
            $this->ajaxReturn(0,"手机号码必须填写！",0,'JSON');
        }
        if (!isset( $_POST [ 'name' ])){
            $this->ajaxReturn(0,"姓名必须填写！",0,'JSON');
        }

        $source1 =  $_POST [ "source1" ];
        $carPinpai =  $_POST [ "carPinpai" ];
        $carType = $_POST [ "carType" ];
        $car =  $_POST [ "car" ];
        $tel = $_POST [ "tel" ];
        $name = $_POST [ "name" ];
        $city = $_POST [ "city" ];
        $ip  =  $_SERVER [ 'REMOTE_ADDR' ];



        $Model = new Model();
        $DataPinpai = $Model->query( "SELECT ClassName from Group_Class where ClassID = '".$carPinpai."'" );
        $DataCarType = $Model->query(  "SELECT ClassName from Group_Class where ClassID = '".$carType."'" );
        $StrCar = "";
        if (isset( $_POST [ 'car' ])){
            $DataCar = $Model->query(  "SELECT Title from Group_Car where id = '".$car."'" );
            $StrCar = " - ". $DataCar[0]["Title"];
        }

        $SqlInset = "INSERT INTO Report3 (UserName, AreaName,Tel,IpAddr,yx,AddDate,source1) VALUES ('".$name."','".$city."','".$tel."','".$ip."','".$DataPinpai[0]["ClassName"]." - ".$DataCarType[0]["ClassName"].$StrCar." ','". date('Y-m-d h:i:s',time())."','".$source1."')";
        $Model->execute($SqlInset);

        $this->ajaxReturn(0,"询价成功！",1,'JSON');
    }

    /**
     * 车型字母分类
     * @return array|mixed
     */
    public function CarZimu(){
        $Model = new Model(); // 实例化一个model对象 没有对应任何数据表
        $StrSqlGC = "Select ID,ClassID,ClassName,Zimu From Group_Class Where ParentID='0' Order By zimu Asc,Orders Asc";
        $DataGc = $Model->query( $StrSqlGC );

        return $DataGc;
    }

    /**
     * 二级车系分类
     * @param $ParentID
     */
    public function CarType($ParentID){
        $Model = new Model(); // 实例化一个model对象 没有对应任何数据表
        $StrSqlCarType = "SELECT ID,ClassID,ClassName,ParentID,ThisPath,Depth from Group_Class where ThisPath LIKE '".$ParentID.",%' ORDER BY ThisPath";
        $DataCarType = $Model->query( $StrSqlCarType );

        if ($DataCarType){
            $this->ajaxReturn($DataCarType,"查询成功！",1,'JSON');
        }else{
            $this->ajaxReturn(0,"没有相关的车系！",0,'JSON');
        }

    }

    /**
     * 三级车型分类
     * @param $ParentID
     */
    public function CarStyle($ParentID){
        $Model = new Model(); // 实例化一个model对象 没有对应任何数据表
        $StrSqlCarStyle = "SELECT ID,Title,ClassID from Group_Car where ClassID = '".$ParentID."' order by ID DESC";
        $DataCarStyle = $Model->query( $StrSqlCarStyle );

        if ($DataCarStyle){
            $this->ajaxReturn($DataCarStyle,"查询成功！",1,'JSON');
        }else{
            $this->ajaxReturn(0,"没有相关的车型！",0,'JSON');
        }
    }

    /**
     * 获取导航的名称
     * @param $ClassID
     * @return string
     */
    public function getClassName($ClassID){
        switch ( $ClassID ) {
            case  "0585365118" :
                $ClassName = "新车";
                break;
            case  "093864742" :
                $ClassName = "降价";
                break;
            case  "0371555104" :
                $ClassName="导购";
                break;
            case "0584076283";
                $ClassName="车讯";
                break;
            case "0374394086":
                $ClassName = "行业";
                break;
            case "050510977":
                $ClassName = "二手";
                break;
            case "025854482";
                $ClassName = "车生活";
                break;
            case "039237296":
                $ClassName = "自驾";
                break;
            case "041539940":
                $ClassName = "法规";
                break;
            default :
                $ClassName="车讯";
                break;
        }

        return $ClassName;

    }
}